package com.zuo.service.serviceImpl;

import com.zuo.entity.Bulletin;
import com.zuo.mapper.BulletinMapper;
import com.zuo.service.BulletinService;
import com.zuo.util.DateFormat;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author 白白旧维
 * @Date 2022/3/3 11:10
 */
@Service
@Transactional(readOnly = true)
public class BulletinServiceImpl implements BulletinService {

    @Autowired
    private BulletinMapper bulletinMapper;

    @Override
    public List<Bulletin> listBulletin() {
        List<Bulletin> bulletinList = bulletinMapper.listBulletin();
        for (Bulletin bulletin:bulletinList){
            bulletin.setCreateTime(DateFormat.format(bulletin.getBcreateTime()));
        }
        return bulletinList;
    }

    @Override
    public List<Bulletin> searchBulletin(String bname, String bcontent) {
        List<Bulletin> bulletinList = bulletinMapper.searchBulletin(bname,bcontent);
        for (Bulletin bulletin:bulletinList){
            bulletin.setCreateTime(DateFormat.format(bulletin.getBcreateTime()));
        }
        return bulletinList;
    }

    @Override
    public Bulletin getOne(int id) {
        return bulletinMapper.getOne(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int updateBulletin(Bulletin bulletin) {
        return bulletinMapper.updateBulletin(bulletin);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int removeBulletins(String ids) {
        String[] id = ids.split(",");
        int count=0;
        for (String i : id) {
            int bid=0;
            if(StringUtils.isNotBlank(i)){
                bid=Integer.parseInt(i);
            }
            int result = bulletinMapper.removeBulletin(bid);
            if(result!=0){
                count++;
            }
        }
        if(count!=id.length){
            throw new RuntimeException("删除失败");
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int addBulletin(Bulletin bulletin) {
        bulletin.setBcreateTime(new Date(System.currentTimeMillis()));
        return bulletinMapper.addBulletin(bulletin);
    }
}
